object InheritanceAndPolymorphismDemo extends App {
  import scala.collection.mutable
  abstract class CampusPerson(
                               val id: String,
                               var name: String,
                               var gender: String,
                               var contact: String
                             ) {
    def showBasicInfo(): Unit = {
      println(s"ID: $id")
      println(s"姓名: $name")
      println(s"性别: $gender")
      println(s"联系方式: $contact")
    }
    def work(): String
  }
  class Student(
                 id: String,
                 name: String,
                 gender: String,
                 contact: String,
                 var major: String,
                 var grade: Int
               ) extends CampusPerson(id, name, gender, contact) {
    private val completedCourses = mutable.Set.empty[String]
    def addCompletedCourse(courseId: String): Unit = {
      completedCourses += courseId
      println(s"学生${name}新增已修课程：$courseId")
    }
    override def work(): String =
      s"学生${name}（${grade}级${major}专业）正在学习，已修课程：${completedCourses.mkString(",")}"
  }
  class Teacher(
                 id: String,
                 name: String,
                 gender: String,
                 contact: String,
                 var department: String,
                 var title: String
               ) extends CampusPerson(id, name, gender, contact) {
    private val teachingCourses = mutable.Set.empty[String]
    def addTeachingCourse(courseId: String): Unit = {
      teachingCourses += courseId
      println(s"教师${name}新增授课课程：$courseId")
    }
    override def work(): String =
      s"教师${name}（${department}${title}）正在授课，授课课程：${teachingCourses.mkString(",")}"
  }
  class Staff(
               id: String,
               name: String,
               gender: String,
               contact: String,
               var office: String,
               var duty: String
             ) extends CampusPerson(id, name, gender, contact) {
    override def work(): String =
      s"行政人员${name}（${office}）正在处理${duty}相关事务"
  }
  val people: List[CampusPerson] = List(
    new Student("S2025001", "张三", "男", "13800138001", "计算机科学", 2025),
    new Teacher("T2025001", "李四", "女", "13900139001", "计算机学院", "副教授"),
    new Staff("ST2025001", "王五", "男", "13700137001", "行政办公室", "学生管理")
  )

  people(0) match {
    case student: Student =>
      student.addCompletedCourse("C101（Scala编程）")
      student.addCompletedCourse("C102（数据库原理）")
    case _ => println("第一个元素不是学生")
  }

  people(1) match {
    case teacher: Teacher =>
      teacher.addTeachingCourse("C101（Scala编程）")
      teacher.addTeachingCourse("C103（人工智能）")
    case _ => println("第二个元素不是教师")
  }
  println("\n=== 校园人员工作状态 ===")
  people.foreach { person =>
    println(person.work())
  }

  println("\n=== 校园人员信息 ===")
  people.foreach { person =>
    println("-------------------")
    person.showBasicInfo()
  }

  println("\n=== 人员类型检查 ===")
  people.foreach { person =>
    val personType = person match {
      case _: Student => "学生"
      case _: Teacher => "教师"
      case _: Staff => "行政人员"
      case _ => "未知类型"
    }
    println(s"${person.name} 是 $personType")
  }
}